package com.dotmedia.dboperations;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dotmedia.core.DatabaseAccessService;
import com.dotmedia.entities.Role;

/**
 * This class is the class which does the database operations for the Role enitity.
 * 
 * @author siddharthsaha
 * 
 */
public class RoleDatabaseOperations {

	private static Logger logger = Logger.getLogger("RoleDatabaseOperations");

	/**
	 * This method persists a role entity into the database
	 * 
	 * @param roleEntity
	 *        The role entity which is to be persisted into the database
	 */
	public void persistRoleEntity(Role roleEntity) {
		logger.info("IN method persistRoleEntity() in RoleDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int persistEntityStatus = databaseAccessService.persistEntity(roleEntity);
		if (persistEntityStatus == 0) {
			logger.info("The entity of Role has been successfully persisted into the database. RoleExtId: " + roleEntity.getRoleExtId());
		} else {
			logger.error("The entity of Role was not successfully persisted into the database. RoleExtId: " + roleEntity.getRoleExtId());
		}
		logger.info("OUT method persistRoleEntity() in RoleDatabaseOperations");
	}

	/**
	 * This method removes a Role entity from the database.
	 * 
	 * @param roleEntity
	 *        The role entity which needs to be removed from the database
	 */
	public void removeRoleEntity(Role roleEntity) {
		logger.info("IN method removeRoleEntity() in RoleDatabaseOperations");
		logger.info("Removing a role entity from database with the role ext id: " + roleEntity.getRoleExtId());
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int removeEntityStatus = databaseAccessService.removeEntity(roleEntity, roleEntity.getId());
		if (removeEntityStatus == 0) {
			logger.info("The entity of Role has been removed from database. RoleExtId: " + roleEntity.getRoleExtId());
		} else {
			logger.error("The entity of Role was not successfully removed from the database. RoleExtId: " + roleEntity.getRoleExtId());
		}
		logger.info("OUT method removeRoleEntity() in RoleDatabaseOperations");
	}

	/**
	 * This method updates a role entity in a database
	 * 
	 * @param roleEntity
	 *        The role entity which needs to be updated
	 */
	public void updateRoleEntity(Role roleEntity) {
		logger.info("IN method updateRoleEntity() in RoleDatabaseOperations");
		String query = "SELECT roleEntity FROM Role roleEntity where roleEntity.id = " + roleEntity.getId();
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		boolean updated = false;
		for (int retreivedEntitiesIndex = 0; retreivedEntitiesIndex < retrievedEntities.size(); retreivedEntitiesIndex++) {
			Role currentRoleEntity = (Role) retrievedEntities.get(retreivedEntitiesIndex);
			if (currentRoleEntity.getId().intValue() == roleEntity.getId().intValue()) {
				logger.info("Got the role entity which needs to be updated");
				currentRoleEntity.setNotes(roleEntity.getNotes());
				currentRoleEntity.setDescription(roleEntity.getDescription());
				currentRoleEntity.setSystem(roleEntity.getSystem());
				int updateEntityStatus = databaseAccessService.updateEntity(currentRoleEntity);
				updated = true;
				if (updateEntityStatus == 0) {
					logger.info("Role entity was successfully updated. RoleExtId: " + roleEntity.getRoleExtId());
				} else {
					logger.error("Role entity cannot be successfully updated. RoleExtId: " + roleEntity.getRoleExtId());
				}
				break;
			}
		}
		if (!updated) {
			logger.warn("Could not get the Role entity in database for updating. RoleExtId: " + roleEntity.getId());
		}
		logger.info("OUT method updateRoleEntity() in RoleDatabaseOperations");
	}

	/**
	 * This method returns a list of Role entities with the role ext id passed to it
	 * 
	 * @param roleExtId
	 *        The roleExtId whose entities is wanted
	 * @return A list of Role entities with roleExtId. There will be only one as the field is unique
	 */
	public List<Role> getRoleEntitiesWithExtId(String roleExtId) {
		List<Role> toReturnRoleEntities = new ArrayList<Role>();
		String query = "SELECT roleEntity FROM Role roleEntity WHERE roleEntity.roleExtId ='" + roleExtId + "'";
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int retrievedEntitiesIndex = 0; retrievedEntitiesIndex < retrievedEntities.size(); retrievedEntitiesIndex++) {
			Role currentRoleEntity = (Role) retrievedEntities.get(retrievedEntitiesIndex);
			toReturnRoleEntities.add(currentRoleEntity);
		}
		return toReturnRoleEntities;
	}

	/**
	 * This method will get all the role entities from the database
	 * 
	 * @return A list of Role objects representing a role in the database
	 */
	public List<Role> getAllRoleEntities() {
		logger.info("IN mehtod getAllRoleEntities() in RoleDatabaseOperations");
		List<Role> toReturnEntities = new ArrayList<Role>();
		String query = "SELECT roleEntity FROM Role roleEntity";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retreivedEntities = databaseAccessService.getEntities(query);
		for (int retrievedEntitiesIndex = 0; retrievedEntitiesIndex < retreivedEntities.size(); retrievedEntitiesIndex++) {
			Role currentEntity = (Role) retreivedEntities.get(retrievedEntitiesIndex);
			toReturnEntities.add(currentEntity);
		}
		logger.info("OUT method getAllRoleEntities() in RoleDatabaseOperations");
		return toReturnEntities;
	}

}
